<?php
namespace Android\Controller;

class AttendController extends BaseController {

    public function submitAttend() {

        $json = urldecode(I ('request.json',''));
        //$json = '{"teachplan_id":1,"timetable_id":4,"value":[{"status":3,"student_id":1}]}';
        //$this->out('RESULT_FAIL',$json);
        if (empty($json))
            $this->out($this->fail,'Empty json');
        // 将获取到得JSON转换为数组
        $jsonArr = json_decode($json,true);
        $data['term_id'] = $this->term['id'];
        $data['teachplan_id'] = $jsonArr['teachplan_id'];
        $teachplan_id = $jsonArr['timetable_id'];

        $data['timetable_id'] = D('Timetable')->getTimeTableId($data['teachplan_id']);
       // dump($data);
        $data['addtime'] = date('Y-m-d H:i:s');

        // 更改课表状态
        $Timetable = M ('Timetable');
        $Timetable->where('id = '.$data['timetable_id'])->setField('status',1);
        // 将数组信息存入数据库
        $Attend = D ('Attend');
        $count = 0;
        foreach ($jsonArr['value'] as $k=>$v) {

            $data['student_id'] = $v['student_id'];
            $data['status'] = $v['status'];
            $flag = $Attend->addAttend($data);
            $flag !== false && $count++;

        }

        // 返回存储结果
        $this->out($this->success,'提交结果成功, 共有'.$count.'个学生被记录',null);
    }


    public function getTeachplanAttend() {

        $teachplan_id = I ('request.teachplan_id',null);
        $term_id = $this->term['id'];

        if (empty($teachplan_id))
            $this->out($this->fail,'请传入一个教学计划标识');

        $Attend = D ('Attend');
        $attendList = $Attend->getAttendByTid($teachplan_id, $term_id);

        if (is_null($attendList))
            $this->out($this->fail, '没有任何考勤记录');

        $Teachplan = D ('Teachplan');
        $courseName = $Teachplan->getCourseName($teachplan_id);
        $courseName = empty($courseName)?"未知":$courseName;

        $Class = D ('Class');

        foreach ($attendList as $k=>$v) {
            $attendList[$k]['student_id'] = (string) $v['student_id'];
            $attendList[$k]['course_name'] = $courseName;
            $attendList[$k]['class_name'] = $Class->getFullClassName($v['class_id']);
        }

        $this->out($this->success, '查询成功', $attendList);
    }


    /**
     * 删除考勤记录
     */
    public function delAttend() {

        $attend_id = (int) I ('request.attend_id',null);
        if (empty($attend_id))
            $this->out($this->fail,'请传入一个考勤标识');

        $Model = M ('Attend');
        $flag = $Model->delete($attend_id);

        if (empty($flag))
            $this->out($this->fail,'删除失败');

        $this->out($this->success,'删除成功');
    }


    /**
     * 修改考勤记录
     */
    public function editAttend() {

        $attend_id = (int) I ('request.attend_id',null);
        $status = (int) I ('request.status',null);

        // 为0不需要记录，直接返回
        if ($status === 0)
            $this->out($this->success, '修改成功');

        if (empty($attend_id))
            $this->out($this->fail,'缺少参数');


        $flag = M ('Attend')->where("id = {$attend_id}")->setField('status',$status);
        if ($flag === false)
            $this->out($this->fail, '修改失败');

        $this->out($this->success, '修改成功');
    }

}